//
//  HDAppointViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/13.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDAppointTableViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface HDAppointTableViewController ()

@end

@implementation HDAppointTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    HDAppointCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell==nil) {
        cell = [[HDAppointCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.orderType = self.orderType;
    cell.delegate = self.delegate;
    cell.customerInfo = self.dataArray[indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [HDAppointCell cellHeight:self.dataArray[indexPath.row]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 7;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

@end

@interface HDAppointCell ()
@property(nonatomic,strong)DOImageView *photo;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *stationTimeLabel;
@property(nonatomic,strong)UILabel *typeLabel;
@property(nonatomic,strong)UILabel *learnTimeLabel;
@property(nonatomic,strong)UILabel *remarkLabel;
@property(nonatomic,strong)UILabel *priceLabel;
@property(nonatomic,strong)UIView *bottomView;
@property(nonatomic,strong)GXButton *locationBtn;
@property(nonatomic,strong)UILabel *distanceLabel;
@property(nonatomic,strong)GXButton *needCompanyBtn;
@end

@implementation HDAppointCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI
{
    UIImageView *line1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 0.5)];
    line1.image = [UIImage imageNamed:@"dot"];
    [self.contentView addSubview:line1];
    [self.contentView addSubview:self.photo];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.stationTimeLabel];
    [self.contentView addSubview:self.typeLabel];
    UIImageView *line2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.photo.bottom+20, APPFRAME.size.width, 0.5)];
    line2.image = [UIImage imageNamed:@"dot"];
    [self.contentView addSubview:line2];
    [self.contentView addSubview:self.learnTimeLabel];
    [self.contentView addSubview:self.remarkLabel];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.bottomView];
    [self.contentView addSubview:self.needCompanyBtn];
}

+ (CGFloat)cellHeight:(HDCustomerInfo *)customerInfo
{
    if (customerInfo.orderType==HDOrderTypePractice) {
        CGSize size = [HDUtil sizeWithString:[NSString stringWithFormat:@"备注：%@",customerInfo.remark] font:[UIFont systemFontOfSize:13] size:CGSizeMake(APPFRAME.size.width-95, CGFLOAT_MAX)];
        if (size.height>15) {
            return 225+size.height;
        }
    }
    return 240;
}

- (void)handleAppointOrder:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(handleAppointOrder:orderId:)])
    {
        [self.delegate performSelector:@selector(handleAppointOrder:orderId:) withObject:sender withObject:self.customerInfo.order_id];
    }
}

- (void)handleAppointOrder:(UIButton *)sender orderId:(NSString *)orderId{}

- (void)setCustomerInfo:(HDCustomerInfo *)value
{
    _customerInfo = [value copy];
    self.photo.imgURLStr = _customerInfo.photoUrl;
    self.nameLabel.text = _customerInfo.name;
    NSString *stationTimeStr = [NSString stringWithFormat:@"平台学时：%@",_customerInfo.stationTime];
    if (self.orderType==HDOrderTypeLearn) {
        stationTimeStr = [NSString stringWithFormat:@"%@  %@",stationTimeStr,_customerInfo.subject];
    }
    self.stationTimeLabel.text = stationTimeStr;
    self.typeLabel.text = [NSString stringWithFormat:@"驾考类型：%@",_customerInfo.type];
    self.learnTimeLabel.text = @"学车时间：";
    if (_customerInfo.startTime!=nil&&_customerInfo.endTime!=nil) {
        NSString *date = [HDUtil formatter:@"M月d日" FromTime:[_customerInfo.startTime doubleValue]];
        NSString *startTime = [HDUtil formatter:@"HH:mm" FromTime:[_customerInfo.startTime doubleValue]];
        NSString *endTime = [HDUtil formatter:@"HH:mm" FromTime:[_customerInfo.endTime doubleValue]];
        self.learnTimeLabel.text = [NSString stringWithFormat:@"学车时间：%@ %@-%@ %@个小时",date,startTime,endTime,[HDUtil check:_customerInfo.learnTime]];
    }
    
    self.needCompanyBtn.hidden = !_customerInfo.needCompany;
    
    if (self.orderType==HDOrderTypePractice) {
        self.remarkLabel.numberOfLines = 0;
        NSString *remarkStr = [NSString stringWithFormat:@"备注：%@",_customerInfo.remark];
        CGSize size = [HDUtil sizeWithString:remarkStr font:[UIFont systemFontOfSize:13] size:CGSizeMake(APPFRAME.size.width-95, CGFLOAT_MAX)];
        self.remarkLabel.height = size.height>15?size.height:15;
        self.remarkLabel.text = remarkStr;
    } else {
        self.remarkLabel.numberOfLines = 1;
        self.remarkLabel.text = [NSString stringWithFormat:@"训练场地：%@",_customerInfo.practicePosition];
    }
    self.bottomView.top = self.remarkLabel.bottom+15;
    self.priceLabel.text = [NSString stringWithFormat:@"%@元",_customerInfo.price];
    [self.locationBtn setTitle:@"" forState:UIControlStateNormal];
    if (_customerInfo.location.length) {
        [self.locationBtn setTitle:_customerInfo.location forState:UIControlStateNormal];
    }
    self.distanceLabel.text = @"";
    if (netEngine.coachInfo.xy.length)
    {
        NSString* xy=netEngine.coachInfo.xy;
        xy=[xy stringByReplacingOccurrencesOfString:@"(" withString:@""];
        xy=[xy stringByReplacingOccurrencesOfString:@")" withString:@""];
        NSArray *array=[xy componentsSeparatedByString:@","];
        CLLocation *coachLocation=[[CLLocation alloc] initWithLatitude:[array[0] floatValue] longitude:[array[1] floatValue]];
        CLLocation *userLocation=[[CLLocation alloc] initWithLatitude:_customerInfo.latitude longitude:_customerInfo.longitude];
        // 计算距离
        CLLocationDistance meters=[coachLocation distanceFromLocation:userLocation];
        self.distanceLabel.text = [NSString stringWithFormat:@"距离我%0.1fKM",meters/1000.0];
        CGSize size = [HDUtil getSizeWithText:self.distanceLabel.text fontSize:13];
        self.distanceLabel.frame = CGRectMake(APPFRAME.size.width-size.width-10, 10, size.width, 20);
    }
}

#pragma mark - getters
- (DOImageView *)photo
{
    if (!_photo) {
        _photo = [[DOImageView alloc] initWithFrame:CGRectMake(10, 15, 50, 50)];
        _photo.layer.cornerRadius = 5;
        _photo.layer.masksToBounds = YES;
        _photo.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _photo;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, 15, APPFRAME.size.width-130, 20)];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textColor = kDarkGrayColor;
    }
    return _nameLabel;
}

- (UILabel *)stationTimeLabel
{
    if (!_stationTimeLabel) {
        _stationTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, self.nameLabel.bottom, APPFRAME.size.width-130, 15)];
        _stationTimeLabel.font = [UIFont systemFontOfSize:12];
        _stationTimeLabel.textColor = [UIColor hexStringToColor:@"#424242"];
    }
    return _stationTimeLabel;
}

- (UILabel *)typeLabel
{
    if (!_typeLabel) {
        _typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, self.stationTimeLabel.bottom, APPFRAME.size.width-130, 15)];
        _typeLabel.font = [UIFont systemFontOfSize:12];
        _typeLabel.textColor = kLightGrayColor;
    }
    return _typeLabel;
}

- (GXButton *)needCompanyBtn
{
    if (!_needCompanyBtn) {
        _needCompanyBtn = [GXButton buttonWithType:UIButtonTypeCustom];
        _needCompanyBtn.frame = CGRectMake(APPFRAME.size.width-83, 30, 73, 25);
        _needCompanyBtn.imageViewFrame = CGRectMake(2, 5, 19, 14);
        _needCompanyBtn.titleLabelFrame = CGRectMake(23, 0, 50, 25);
        _needCompanyBtn.relayout = YES;
        [_needCompanyBtn setTitle:@"需要接送" forState:UIControlStateNormal];
        [_needCompanyBtn setImage:[UIImage imageNamed:@"汽车.jpg"] forState:UIControlStateNormal];
        [_needCompanyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _needCompanyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        _needCompanyBtn.userInteractionEnabled = NO;
        _needCompanyBtn.hidden = YES;
    }
    return _needCompanyBtn;
}

- (UILabel *)learnTimeLabel
{
    if (!_learnTimeLabel) {
        _learnTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.photo.bottom+30, APPFRAME.size.width-20, 15)];
        _learnTimeLabel.font = [UIFont systemFontOfSize:13];
        _learnTimeLabel.textColor = kDarkGrayColor;
    }
    return _learnTimeLabel;
}

- (UILabel *)remarkLabel
{
    if (!_remarkLabel) {
        _remarkLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.learnTimeLabel.bottom+10, APPFRAME.size.width-95, 15)];
        _remarkLabel.font = [UIFont systemFontOfSize:13];
        _remarkLabel.textColor = kDarkGrayColor;
    }
    return _remarkLabel;
}

- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(APPFRAME.size.width-70, self.learnTimeLabel.bottom+5, 60, 30)];
        _priceLabel.textColor = [UIColor hexStringToColor:@"#ff2b01"];
        _priceLabel.font = [UIFont systemFontOfSize:13];
    }
    return _priceLabel;
}

- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.remarkLabel.bottom+15, APPFRAME.size.width, 90)];
        UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 0.5)];
        topLine.image = [UIImage imageNamed:@"dot"];
        [_bottomView addSubview:topLine];
        UIImageView *bottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 90, APPFRAME.size.width, 0.5)];
        bottomLine.image = [UIImage imageNamed:@"dot"];
        [_bottomView addSubview:bottomLine];
        [_bottomView addSubview:self.locationBtn];
        [_bottomView addSubview:self.distanceLabel];
        UIButton *rejectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        rejectBtn.frame = CGRectMake(15, self.locationBtn.bottom+15, 80, 30);
        [rejectBtn setBackgroundImage:[UIImage imageNamed:@"拒绝"] forState:UIControlStateNormal];
        [rejectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rejectBtn setTitle:@"拒绝" forState:UIControlStateNormal];
        rejectBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        rejectBtn.tag = 2000;
        [rejectBtn addTarget:self action:@selector(handleAppointOrder:) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:rejectBtn];
        
        UIButton *acceptBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        acceptBtn.frame = CGRectMake(APPFRAME.size.width-95, self.locationBtn.bottom+15, 80, 30);
        [acceptBtn setBackgroundImage:[UIImage imageNamed:@"需要接送"] forState:UIControlStateNormal];
        [acceptBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [acceptBtn setTitle:@"接单" forState:UIControlStateNormal];
        acceptBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        acceptBtn.tag = 2001;
        [acceptBtn addTarget:self action:@selector(handleAppointOrder:) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:acceptBtn];
    }
    return _bottomView;
}

- (GXButton *)locationBtn
{
    if (!_locationBtn) {
        _locationBtn = [GXButton buttonWithType:UIButtonTypeCustom];
        _locationBtn.frame = CGRectMake(10, 10, APPFRAME.size.width-20, 20);
        _locationBtn.imageViewFrame = CGRectMake(5, 3, 9, 13);
        _locationBtn.titleLabelFrame = CGRectMake(23, 0, APPFRAME.size.width-43, 20);
        [_locationBtn setTitleColor:kDarkGrayColor forState:UIControlStateNormal];
        _locationBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        _locationBtn.relayout = YES;
        _locationBtn.userInteractionEnabled = NO;
        [self.locationBtn setImage:[UIImage imageNamed:@"定位"] forState:UIControlStateNormal];
    }
    return _locationBtn;
}

- (UILabel *)distanceLabel
{
    if (!_distanceLabel) {
        _distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(APPFRAME.size.width-90, 10, 90, 20)];
        _distanceLabel.textAlignment = NSTextAlignmentRight;
        _distanceLabel.textColor = kDarkGrayColor;
        _distanceLabel.font = [UIFont systemFontOfSize:13];
    }
    return _distanceLabel;
}

@end
